using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WbsEditor.Generation
{
    public enum GenerationMode
    {
        PersonalizedWbs = 0,
        WorkloadAnalysis = 1
    }

    public partial class WbsGeneratorSettings : Form
    {
        public WbsGeneratorSettings(WbsTfsClient tfsClient)
        {
            TfsClient = tfsClient;
            InitializeComponent();
        }

        private WbsTfsClient _tfsClient;
        public WbsTfsClient TfsClient
        {
            get { return _tfsClient; }
            set { _tfsClient = value; }
        }

        public string UserName
        {
            get { return _userCombo.Text; }
            set { _userCombo.Text = value; }
        }

        public bool IncludeOrphanTasks
        {
            get { return _includeOrphansCheck.Checked; }
        }

        public bool IncludeClosedWorkItems
        {
            get { return _includeClosedCheck.Checked; }
        }

        public bool IncludeBugs
        {
            get { return _includeBugsCheck.Checked; } 
        }

        public bool RefreshWbsNodes
        {
            get { return _refreshWbsNodes.Checked; }
        }

        public string[] SelectedIterations
        {
            get
            {
                var items = _iterationListBox.CheckedItems;
                string[] itemStrings = new string[items.Count];
                items.CopyTo(itemStrings, 0);
                return itemStrings;
            }
        }

        private GenerationMode _generationMode = GenerationMode.PersonalizedWbs;
        public GenerationMode GenerationMode
        {
            get { return _generationMode; }
            set
            {
                _generationMode = value;
                if (GenerationMode.PersonalizedWbs == _generationMode)
                {
                    Text = "Generate WBS Settings";
                    _mainLabel.Text = "This function generates a read-only, personalized Work Breakdown Structure for a given Team Project and a given Iteration.";
                    _userLabel.Visible = true;
                    _userCombo.Visible = true;
                    _includeClosedCheck.Visible = true;
                }
                else
                {
                    Text = "Generate Workload Chart";
                    _mainLabel.Text = "This function generates a chart of all remaining work for all WBS work items in the given Iteration and Team Project.";
                    _userLabel.Visible = false;
                    _userCombo.Visible = false;
                    _includeClosedCheck.Visible = false;
                }
            }
        }

        private void WbsGeneratorSettings_Load(object sender, EventArgs e)
        {
            string[] teamProjects = TfsClient.GetTeamProjectNames();
            _teamCombo.Items.AddRange(teamProjects);
            string teamProject = TfsClient.TeamProject;
            _teamCombo.Text = teamProject;
            if (!string.IsNullOrEmpty(teamProject))
            {
                string[] iterations = TfsClient.GetTeamProjectIterations();
                //_iterationCombo.Items.AddRange(iterations);
                //_iterationCombo.Text = TfsClient.Iteration;
                _iterationListBox.Items.AddRange(iterations);
                try
                {
                    int iterationIndex = Array.IndexOf(iterations, TfsClient.Iteration);
                    _iterationListBox.SetItemChecked(iterationIndex, true);
                    int visibleIndex = iterationIndex > 2 ? iterationIndex - 2 : 0;
                    _iterationListBox.TopIndex = visibleIndex;
                }
                catch (Exception)
                {
                }

                string[] names = TfsClient.GetPossibleAssignedToNames();
                _userCombo.Items.AddRange(names);
            }
        }

        private void _teamCombo_TextChanged(object sender, EventArgs e)
        {
            string newProject = _teamCombo.Text;
            if (!string.IsNullOrEmpty(TfsClient.TeamProject))
            {
                if (TfsClient.TeamProject.Equals(newProject))
                    return;
            }

            TfsClient.TeamProject = newProject;
            string[] iterations = TfsClient.GetTeamProjectIterations();
            //_iterationCombo.Items.Clear();
            //_iterationCombo.Items.AddRange(iterations);
            //_iterationCombo.Text = "";
            _iterationListBox.Items.Clear();
            _iterationListBox.Items.AddRange(iterations);
            try
            {
                _iterationListBox.SelectedItem = null;
            }
            catch (Exception)
            {
            }
            TfsClient.Iteration = "";

            string[] names = TfsClient.GetPossibleAssignedToNames();
            string oldName = _userCombo.Text;
            _userCombo.Items.Clear();
            _userCombo.Items.AddRange(names);
        }

        private void _generateButton_Click(object sender, EventArgs e)
        {
            string[] selectedItems = SelectedIterations;
            if (selectedItems.Length == 0)
            {
                MessageBox.Show(this, "Please select one or more iterations to run the analysis for.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult = DialogResult.OK;
            Dispose();
        }

        //private void _iterationCombo_TextChanged(object sender, EventArgs e)
        //{
        //    TfsClient.Iteration = _iterationCombo.Text;
        //}
    }
}